package com.atomscat.lottery.service;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import org.springframework.stereotype.Service;

import java.util.Map;

@Slf4j
@Service
@RequiredArgsConstructor
public class ActivityInvitationService {

    public void sendKefuMessage(WxMpXmlMessage wxMpXmlMessage, Map<String, Object> map,
                                WxMpService wxMpService) {
        try {
            String lotteryActivityId = wxMpXmlMessage.getEventKey().replaceAll("qrscene_","");
            String appId = wxMpService.getWxMpConfigStorage().getAppId();
            String host = wxMpService.getWxMpConfigStorage().getOauth2RedirectUrl();
            String openId = wxMpXmlMessage.getFromUser();
            String url = "https://open.weixin.qq.com/connect/oauth2/authorize?" +
                    "appid=" + appId + "&" +
                    "redirect_uri=" + host + "%2Fapi%2Fwechat%2Fredirect%2F" + appId + "%2Flottery%2F" + lotteryActivityId + "&" +
                    "response_type=code&" +
                    "scope=snsapi_userinfo&" +
                    "state=123#wechat_redirect";
            WxMpKefuMessage.WxArticle article = new WxMpKefuMessage.WxArticle();
            article.setUrl(url);
            article.setPicUrl("PIC_URL");
            article.setDescription("丰厚大奖等你来！！！");
            article.setTitle("点击参与抽奖");

            wxMpService.getKefuService().sendKefuMessage(WxMpKefuMessage.NEWS()
                    .toUser(openId)
                    .addArticle(article)
                    .build());

        }  catch (WxErrorException e) {
            log.error(e.getMessage(), e);
        }
    }


}
